package com.jacen.fzxs.util;

import java.io.StringReader;
import java.io.StringWriter;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlFormat {

	public static String format(String str) throws Exception {
		SAXReader reader = new SAXReader();
		// 创建一个串的字符输入流
		StringReader in = new StringReader(str);
		Document doc = reader.read(in);
		// System.out.println(doc.getRootElement());
		// 创建输出格式
		OutputFormat formater = OutputFormat.createPrettyPrint();
		// formater=OutputFormat.createCompactFormat();
		// 设置xml的输出编码
		formater.setEncoding("utf-8");
		// 创建输出(目标)
		StringWriter out = new StringWriter();
		// 创建输出流
		XMLWriter writer = new XMLWriter(out, formater);
		// 输出格式化的串到目标中，执行后。格式化后的串保存在out中。
		writer.write(doc);

		writer.close();
		System.out.println(out.toString());
		// 返回我们格式化后的结果
		return out.toString();
	}
	
	public static void main(String[] args) {
		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	+"<response><responseState><code>0</code></responseState><data><exchange><exchangeNo>150625000845</exchangeNo><exchangeType>1</exchangeType><exchangeName>销售</exchangeName><cardNo>1027</cardNo><oriAmount>860.00</oriAmount><transTime>2015-06-25 19:29:53</transTime><terminalNo>00000001</terminalNo><cashierNo>000005</cashierNo><cashierName>赵永萍  </cashierName><paymentList><payment><traceDetailId>202</traceDetailId><paymentName>现金</paymentName><paymentType>8011</paymentType><acceptSum>100.00</acceptSum><paidCash>100.00</paidCash><cashChange>0.00</cashChange></payment><payment><traceDetailId>203</traceDetailId><paymentName>银行卡</paymentName><paymentType>8021</paymentType><cardNo>6225676888199911</cardNo><acceptSum>720.00</acceptSum><referenceNo>10010101010</referenceNo><cardName>工行</cardName></payment><payment><traceDetailId>204</traceDetailId><paymentName>会员积分</paymentName><paymentType>8061</paymentType><cardNo>1027</cardNo><acceptSum>40.00</acceptSum><useAmount>4000.00</useAmount><asMoneyRate>0.01</asMoneyRate></payment></paymentList><goodsDetailList><goodsDetail><price>360.00</price><quantity>1</quantity><brandName>伊斯娜瑞服饰  </brandName><slipNo>150625000841</slipNo><profits>10.00</profits><cardNo>1027</cardNo></goodsDetail><goodsDetail><price>500.00</price><quantity>1</quantity><brandName>adidas童装</brandName><slipNo>150625000841</slipNo><profits>0.00</profits><cardNo>1027</cardNo></goodsDetail></goodsDetailList><saleSlipList><saleSlip><exchangeNo>150625000845</exchangeNo><exchangeType>1</exchangeType><exchangeName>销售</exchangeName><transTime>2015-06-25 19:29:53</transTime><terminalNo>00000001</terminalNo><cashierNo>000005</cashierNo><cashierName>赵永萍  </cashierName><slipNo>150625000841</slipNo><oriAmount>860.00</oriAmount><profits>10.00</profits><givePoint>0</givePoint><cardNo>1027</cardNo><paymentList><payment><traceDetailId>199</traceDetailId><paymentName>现金</paymentName><paymentType>8011</paymentType><acceptSum>100.00</acceptSum><paidCash>100.00</paidCash><cashChange>0.00</cashChange></payment><payment><traceDetailId>200</traceDetailId><paymentName>银行卡</paymentName><paymentType>8021</paymentType><cardNo>6225676888199911</cardNo><acceptSum>720.00</acceptSum><referenceNo>10010101010</referenceNo><cardName>工行</cardName></payment><payment><traceDetailId>201</traceDetailId><paymentName>会员积分</paymentName><paymentType>8061</paymentType><cardNo>1027</cardNo><acceptSum>40.00</acceptSum><useAmount>4000.00</useAmount><asMoneyRate>0.01</asMoneyRate></payment></paymentList><goodsDetailList><goodsDetail><price>360.00</price><quantity>1</quantity><brandName>伊斯娜瑞服饰  </brandName><slipNo>150625000841</slipNo><profits>10.00</profits><cardNo>1027</cardNo></goodsDetail><goodsDetail><price>500.00</price><quantity>1</quantity><brandName>adidas童装</brandName><slipNo>150625000841</slipNo><profits>0.00</profits><cardNo>1027</cardNo></goodsDetail></goodsDetailList></saleSlip></saleSlipList></exchange></data></response>";
		try {
			XmlFormat.format(str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
